# filename: stock_price_comparison.py
import pandas as pd
import matplotlib.pyplot as plt
import yfinance as yf

# Define the symbols for NVIDIA and Tesla
nvda_symbol = "NVDA"
tesla_symbol = "TSLA"

# Define the start and end dates for the stock price data
start_date = "2022-01-01" # Change the start date if needed
end_date = pd.Timestamp.today().strftime("%Y-%m-%d")

# Fetch the stock price data from Yahoo Finance
nvda_data = yf.download(nvda_symbol, start=start_date, end=end_date)
tesla_data = yf.download(tesla_symbol, start=start_date, end=end_date)

# Filter the "Close" column from the stock price data
nvda_close = nvda_data["Close"]
tesla_close = tesla_data["Close"]

# Calculate the YTD stock price changes
nvda_ytd_change = (nvda_close[-1] - nvda_close[0]) / nvda_close[0] * 100
tesla_ytd_change = (tesla_close[-1] - tesla_close[0]) / tesla_close[0] * 100

# Plot the YTD stock price changes
plt.figure(figsize=(10, 6))
plt.plot(nvda_close, label="NVDA")
plt.plot(tesla_close, label="TSLA")
plt.title("YTD Stock Price Changes - NVDA vs TSLA")
plt.xlabel("Date")
plt.ylabel("Stock Price")
plt.legend(loc="upper left")
plt.grid(True)
plt.show()

# Print the YTD stock price changes
print("NVDA YTD Change: {:.2f}%".format(nvda_ytd_change))
print("TSLA YTD Change: {:.2f}%".format(tesla_ytd_change))